﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Warensoft.Unity.Communication.Client.Models;

namespace Warensoft.Unity.Communication.Client.OData
{
    /// <summary>
    /// XML 实体封装器
    /// </summary>
    public class ODataEntity
    {
        private XmlNode doc = null;
        
      
        public Dictionary<string,ODataProperty> Properties { get; set; }
        public Dictionary<string, ODataTable> ChildTables { get; set; }
        private string accessUrl = "";
        /// <summary>
        /// 该数据的访问URL
        /// </summary>
        public string AccessUrl
        {
            get
            {
                return this.accessUrl;
            }
        }
       public ODataProperty this[int index]
       {
           get
           {
               if (index<0||index>this.Properties .Count -1)
               {
                   return null;
               }
               return this.Properties.Skip(index).Take(1).First().Value ;
           }
       }
       public ODataProperty this[string key]
       {
           get
           {
               if (this.Properties .ContainsKey(key))
               {
                   return this.Properties[key];
               }
               return null;
           }
       }
        public ODataContext currentContext;
        public ODataEntity(XmlNode xml, XmlNamespaceManager nsmgr,List<ODataProperty>propertyDefinations,List<ODataNavigation>navigationDefinations,ODataContext context)
        {
            this.currentContext =context;
            this.doc = xml;
            this.accessUrl = this.doc.SelectSingleNode("warensoft:id", nsmgr).InnerText;
           
            this.ChildTables = new Dictionary<string, ODataTable>();
            foreach (var nav in navigationDefinations)
            {
                this.ChildTables.Add(nav.Name, new ODataTable(nav.Name, this.accessUrl + "/" + nav.Name, true, this.currentContext.Tables[nav.Name].PropertDefinations, this.currentContext.Tables[nav.Name].NavigationDefinations,this.currentContext ));
            }


            var ps = this.doc.SelectSingleNode("warensoft:content", nsmgr).ChildNodes[0].ChildNodes.Cast<XmlNode>()
                .Select(node => new
                {
                    Name = node.Name.Remove(0, 2),
                    Value = node.InnerText
                });
            this.Properties = propertyDefinations.ToDictionary(p=>p.Name );
            foreach (var item in ps)
            {

                this.Properties[item.Name].Value = item.Value;
            }


        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var p in this.Properties)
            {
                sb.Append(p.Key);
                sb.Append(":");
                sb.Append(p.Value.Value.ToString());
                sb.Append(",");
            }
            return sb.ToString();
        }
   
    }
}
